package com.hfzy.ihk.common.core.mybatis.typeHandler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 自定义Boolean类型转换器.
 */
@SuppressWarnings("rawtypes")
public class Boolean2IntTypeHandler implements TypeHandler<Boolean> {

	/** (non-Javadoc)
	 * @see TypeHandler#getResult(ResultSet, String)
	 */
	@Override
	public Boolean getResult(ResultSet arg0, int index) throws SQLException {
		int num = arg0.getInt(index);
		Boolean rt = Boolean.FALSE;
		if (num == 1){
			rt = Boolean.TRUE;
		}
		return rt;
	}

	/** (non-Javadoc)
	 * @see TypeHandler#getResult(CallableStatement, int)
	 */
	@Override
	public Boolean getResult(CallableStatement arg0, int index)
			throws SQLException {
		int num = arg0.getInt(index);
		Boolean rt = Boolean.FALSE;
		if (num == 1){
			rt = Boolean.TRUE;
		}
		return rt;
	}

	@Override
	public Boolean getResult(ResultSet arg0, String columnName) throws SQLException {
		int num = arg0.getInt(columnName);
		Boolean rt = Boolean.FALSE;
		if (num == 1){
			rt = Boolean.TRUE;
		}
		return rt;
	}

	/** (non-Javadoc)
	 * @see TypeHandler#setParameter(PreparedStatement, int, Object, JdbcType)
	 */
	@Override
	public void setParameter(PreparedStatement arg0, int arg1, Boolean param,
							 JdbcType arg3) throws SQLException {
		int value = param == true ? 1 : 0;
		arg0.setInt(arg1, value);
	}
}
